package com.marion.langchain4jdemo.config;

import com.marion.langchain4jdemo.service.DeviceService;
import com.marion.langchain4jdemo.service.QWen;
import com.marion.langchain4jdemo.service.RepairRecordService;
import com.marion.langchain4jdemo.service.SearchService;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 注册工具到MCP Server
 */
@Configuration
public class ToolConfig {
    @Bean
    public ToolCallbackProvider tools(SearchService searchService,
                                      DeviceService deviceService,
                                      RepairRecordService repairRecordService) {
        return MethodToolCallbackProvider.builder().
                toolObjects(searchService, deviceService, repairRecordService)
                .build();
    }

    @Bean
    public QWen Qwen_Assistant() {
        
        String s =" sadasdasd ";
        ChatLanguageModel model = OpenAiChatModel.builder()
                .apiKey("sk-0c04513556c048a396c37909e926e172")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .modelName("qwen-plus")
                .build();

        // 创建 ChatMemoryProvider，按用户ID分配独立聊天记忆
        ChatMemoryProvider memoryProvider = userId ->
                MessageWindowChatMemory.builder()
                        .id(userId)  // 用户唯一标识（如用户ID、会话ID）
                        .maxMessages(10) // 保留最近10条消息
                        .chatMemoryStore(new InMemoryChatMemoryStore()) // 存储方式（内存或持久化）
                        .build();

        return AiServices.builder(QWen.class).chatLanguageModel(model).
                chatMemoryProvider(memoryProvider)
                .build();
    }
}
